﻿<MetroControls:MetroWindow x:Class="WindowsPhonePowerTools.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WindowsPhonePowerTools"
        xmlns:MetroControls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:MetroUI="clr-namespace:MetroUI;assembly=MetroUI"                           
        xmlns:stock="clr-namespace:StockIcons"
        Title="WP POWER TOOLS" Height="300" Width="300" MinHeight="400" MinWidth="750"
        ResizeMode="CanResizeWithGrip"
        Closing="TheWindow_Closing"
        x:Name="TheWindow"
        Icon="wptools.ico">

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MetroUI;component/Themes/Generic.xaml" />

                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedTabControl.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />

                <ResourceDictionary>

                    <local:FileTypeToIconConverter x:Key="FileTypeToIconConverter"></local:FileTypeToIconConverter>
                    <local:StreamToBitmapImageConverter x:Key="StreamToBitmapImageConverter"></local:StreamToBitmapImageConverter>
                    <local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"></local:BoolToVisibilityConverter>

                    <Style x:Key="ActionButton" TargetType="MetroControls:Tile">
                        <Setter Property="Height" Value="70"></Setter>
                        <Setter Property="Width" Value="100"></Setter>
                    </Style>

                    <Style x:Key="MetroSmallHeader" TargetType="TextBlock">
                        <Setter Property="FontSize" Value="10"></Setter>
                        <Setter Property="FontWeight" Value="Bold"></Setter>
                    </Style>

                    <Style x:Key="MetroLink" TargetType="Hyperlink">
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Foreground" Value="Gray" />
                            </Trigger>
                        </Style.Triggers>
                        <Setter Property="Foreground" Value="Black"></Setter>
                        <Setter Property="FontSize" Value="12"></Setter>
                        <Setter Property="FontFamily" Value="Segoe UI, Verdana"></Setter>
                    </Style>

                    <Style x:Key="DeviceInfoHeader" TargetType="TextBlock">
                        <Setter Property="Margin" Value="0,0,12,0"></Setter>
                    </Style>

                    <Style x:Key="DeviceInformationSeparator" TargetType="Separator">
                        <Setter Property="Margin" Value="-10,3,-10,3"></Setter>
                    </Style>

                    <ControlTemplate x:Key="NoScrollList">
                        <ItemsPresenter></ItemsPresenter>
                    </ControlTemplate>
                </ResourceDictionary>

            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <MetroControls:MetroWindow.WindowCommands>
        <MetroControls:WindowCommands>
            <StackPanel Orientation="Horizontal">
                <Button BorderThickness="0" Content="connect to a device" Click="btnLaunchConnect_Click" />
                <Button BorderThickness="0">
                    <Button Content="about" Click="btnAbout_Click" />
                </Button>
                <Button BorderThickness="0">
                    <Button Content="settings" Click="btnSettings_Click" />
                </Button>
            </StackPanel>
        </MetroControls:WindowCommands>
    </MetroControls:MetroWindow.WindowCommands>

    <Border BorderThickness="0" BorderBrush="Red">
        <Grid Margin="12, 20, 20, 0">
            <MetroUI:NavigationMenu x:Name="navigator">

                <MetroUI:NavigationButton Source="/images/navbuttons/install_update.png" Text="Install | Update" OnSelectionChanged="NavigationButton_OnSelectionChanged" x:Name="btnInstallUpdate">
                    <Grid x:Name="pivotInstall" IsEnabled="{Binding Device.Connected}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                        <Grid>
                            <Grid Margin="6,35,0,0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <TextBox Height="22" Grid.Column="0" Margin="0,0,12,0" Name="txtXapFile" VerticalAlignment="Top" />
                                <Button Content="Browse" Height="24" HorizontalAlignment="Left" Grid.Column="1" Name="btnBrowse" VerticalAlignment="Top" Width="77" Click="btnBrowse_Click" />
                            </Grid>
                                                        
                            <Button Content="Install" Height="24" HorizontalAlignment="Left" Margin="8,70,0,0" Name="btnInstall" VerticalAlignment="Top" Width="77" Click="btnInstall_Click" />
                            <Button Content="Update" Height="24" HorizontalAlignment="Left" Margin="104,70,0,0" Name="btnUpdate" VerticalAlignment="Top" Width="77" Click="btnUpdate_Click" />
                            <TextBlock Height="23" HorizontalAlignment="Left" Margin="8,15,0,0" Name="textBlock1" VerticalAlignment="Top" Width="513">XAP Path (select multiple files by hitting browse, or by separating names with a semi-colon (;) ):</TextBlock>
                            <TextBlock Height="81" HorizontalAlignment="Left" Margin="8,105,0,0" Name="textBlock2" TextWrapping="Wrap" VerticalAlignment="Top" Width="488">
                        &#8226; Failures are silent - Updating a xap that is not already installed will have no effect<LineBreak/>
                        &#8226; Installing a xap that already exists will wipe the existing installation and reinstall the xap
                                <LineBreak/><LineBreak/>
                                You can also add previously used xaps:
                            </TextBlock>

                            <Grid Margin="6,175,0,0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>

                                <ListBox Grid.Column="0" Margin="0,0,12,12" Name="listPreviousXaps" VerticalAlignment="Top" SelectionMode="Extended" ItemsSource="{Binding PreviousXapPaths}" MouseDoubleClick="listPreviousXaps_MouseDoubleClick"></ListBox>
                                <Button Content="Add" Height="24" HorizontalAlignment="Left" Grid.Column="1" Name="btnAddPreviousXap" VerticalAlignment="Top" Width="77" Click="btnAddPreviousXap_Click" />
                            </Grid>
                        </Grid>
                    </Grid>
                </MetroUI:NavigationButton>
                <MetroUI:NavigationButton Source="/images/navbuttons/developer_apps.png" Text="Dev Apps"  OnSelectionChanged="NavigationButton_OnSelectionChanged">
                    <Canvas x:Name="pivotApps" IsEnabled="{Binding Device.Connected}">
                        <Canvas>
                            <TextBlock Margin="6,11,349,243" Style="{StaticResource MetroSmallHeader}" Canvas.Top="1">INSTALLED APPS</TextBlock>
                            <ListBox Height="235" HorizontalAlignment="Left" Margin="6,32,0,0" Name="lstInstalledApps" VerticalAlignment="Top" Width="242" ItemsSource="{Binding Device.InstalledApplications}" SelectedItem="{Binding CurSelectedInstalledApp, Mode=TwoWay}" Canvas.Top="-2">
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal" Margin="0,1.5,0,1.5">
                                            <Image Height="20" Width="20" Margin="0,0,4,0" Source="{Binding Icon, Converter={StaticResource StreamToBitmapImageConverter}}"></Image>
                                            <TextBlock Text="{Binding Name}"></TextBlock>
                                        </StackPanel>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
                            <StackPanel Canvas.Left="260" Canvas.Top="30">
                                <TextBlock Height="20" Text="GUID" Style="{StaticResource MetroSmallHeader}" />
                                <TextBlock Height="22" Name="txtAppGuid" Width="232" Text="{Binding CurSelectedInstalledApp.RemoteApplication.ProductID}" />
                                <TextBlock Height="20" Text="NAME" Style="{StaticResource MetroSmallHeader}" />
                                <TextBox Height="22" Name="txtAppName" Width="232" Text="{Binding CurSelectedInstalledApp.Name, Mode=TwoWay}" />
                                <TextBlock TextWrapping="Wrap" Width="250">App names cannot be read off the device and are populated when you install an app. You can change an app name by editing it above.</TextBlock>
                            </StackPanel>
                            <GroupBox Canvas.Left="254" Canvas.Top="203" Header="ACTIONS" FontWeight="Bold" FontSize="10" Height="62" Width="246">
                                <Canvas>
                                    <Button Content="Uninstall" Height="23" Name="btnUninstall" Width="65" Click="btnUninstall_Click" Canvas.Left="7" Canvas.Top="6" />
                                    <Button Content="Launch" Height="23" x:Name="btnLaunchApp" Click="btnLaunchApp_Click" Canvas.Left="89" Canvas.Top="6" Width="60"></Button>
                                    <Button Content="Kill" Height="23" x:Name="btnKillApp" Click="btnKillApp_Click" Canvas.Left="164" Canvas.Top="6" Width="60"></Button>
                                </Canvas>
                            </GroupBox>
                        </Canvas>
                        <TextBlock Canvas.Left="7" Canvas.Top="280">Note: App names will appear as GUIDs unless they have been installed or updated with this tool</TextBlock>
                    </Canvas>
                </MetroUI:NavigationButton>
                <MetroUI:NavigationButton Source="/images/navbuttons/iso_store.png" Text="Isolated Storage"  OnSelectionChanged="NavigationButton_OnSelectionChanged">
                    <Grid x:Name="pivotFileBrowser" IsEnabled="{Binding Device.Connected}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="5"></RowDefinition>
                            <RowDefinition Height="280*"></RowDefinition>
                            <RowDefinition Height="40"></RowDefinition>
                            <RowDefinition Height="30"></RowDefinition>
                            <RowDefinition Height="30*"></RowDefinition>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="390*"></ColumnDefinition>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                        </Grid.ColumnDefinitions>

                        <TreeView Grid.Row="1" Grid.Column="0" Margin="6,0,10,0" MinHeight="210" MinWidth="365" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="treeIsoStore" SelectedItemChanged="treeIsoStore_SelectedItemChanged" ItemsSource="{Binding Device.RemoteIsoStores}" TreeViewItem.Expanded="treeIsoStore_Expanded">
                            <TreeView.ItemContainerStyle>
                                <Style TargetType="{x:Type TreeViewItem}">
                                    <EventSetter Event="MouseDoubleClick" Handler="treeIsoStoreItem_OnDoubleClick"></EventSetter>
                                    <EventSetter Event="KeyUp" Handler="treeIsoStoreItem_OnKeyUp"></EventSetter>
                                    <Setter Property="AllowDrop" Value="True"></Setter>
                                    <EventSetter Event="Drop" Handler="treeIsoStoreItem_OnDrop"></EventSetter>
                                    <EventSetter Event="MouseMove" Handler="treeIsoStoreItem_OnMouseMove"></EventSetter>
                                </Style>
                            </TreeView.ItemContainerStyle>
                            <TreeView.ItemTemplate>
                                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                                    <StackPanel Orientation="Horizontal">
                                        <StackPanel.ContextMenu>
                                            <ContextMenu>
                                                <MenuItem Header="Refresh" x:Name="btnRefresh" Click="btnRefresh_Click" />
                                                <MenuItem Header="Launch" x:Name="btnLaunchMenu" Click="btnLaunchMenu_Click"/>
                                            </ContextMenu>
                                        </StackPanel.ContextMenu>
                                        <Image Height="16" Width="16" Source="{Binding Icon, Converter={StaticResource FileTypeToIconConverter}}" Margin="0,1,5,3"></Image>
                                        <TextBlock Text="{Binding Name}"></TextBlock>
                                    </StackPanel>
                                </HierarchicalDataTemplate>
                            </TreeView.ItemTemplate>
                        </TreeView>
                        <StackPanel Grid.Row="1" Grid.Column="1" Grid.RowSpan="3" x:Name="stackFileProperties" Width="145">
                            <TextBlock Height="19" Text="CREATED" Style="{StaticResource MetroSmallHeader}" />
                            <TextBlock Height="23" Text="{Binding CreationTime}" />
                            <TextBlock Height="19" Text="ATTRIBUTES" Style="{StaticResource MetroSmallHeader}" />
                            <TextBlock Height="23" Text="{Binding FileAttribute}" />
                            <TextBlock Height="19" Text="LAST ACCESSED" Style="{StaticResource MetroSmallHeader}" />
                            <TextBlock Height="23" Text="{Binding LastWriteTime}" />
                            <TextBlock Height="19" Text="LAST WRITE" Style="{StaticResource MetroSmallHeader}" />
                            <TextBlock Height="23" Text="{Binding LastAccessTime}" />
                            <TextBlock Height="19" Text="SIZE" Style="{StaticResource MetroSmallHeader}" />
                            <TextBlock Height="23" Text="{Binding Length}" />
                        </StackPanel>

                        <StackPanel Grid.Row="2" Orientation="Horizontal" Margin="0,5,0,0">
                            <Button Content="Get" Height="24" Name="btnGet" Width="77" Click="btnGet_Click" Margin="10,0,10,0" />
                            <Button Content="Put File" Height="24" Name="btnPutFile" Width="77" Click="btnPutFile_Click" Margin="0,0,10,0" />
                            <Button Content="Put Directory" Height="24" Name="btnPutDirectory" Width="92" Click="btnPutDirectory_Click" Margin="0,0,10,0" />
                            <Button Content="Delete" Height="24" Name="btnDelete" Width="77" Click="btnDelete_Click" />
                        </StackPanel>

                        <CheckBox Grid.Row="3" Canvas.Left="7" Margin="5,0,0,0" Content="Overwrite Existing" Height="16" Name="chkOverwrite" />

                        <TextBlock Grid.Row="4" Grid.ColumnSpan="2" Margin="5,0,0,0">Note: App names will appear as GUIDs unless they have been installed or updated with this tool</TextBlock>
                    </Grid>
                </MetroUI:NavigationButton>
            </MetroUI:NavigationMenu>

            <MetroUI:MetroDialog x:Name="dialogError" Visibility="Collapsed" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <Canvas>
                    <TextBlock FontSize="20" FontWeight="Bold" Canvas.Left="54">Oh Oh! An error has occurred</TextBlock>
                    <TextBlock Canvas.Left="16" Canvas.Top="55" Width="370" Text="{Binding ErrorString}" FontSize="16" TextWrapping="Wrap"></TextBlock>
                    <Button x:Name="btnDismissErrorDialog" Content="OK" Click="btnDismissErrorDialog_Click" Height="22" Width="73" FontFamily="Segoe WP" Canvas.Left="311" Canvas.Top="150"></Button>
                </Canvas>
            </MetroUI:MetroDialog>

            <!-- TODO: move to some kind of binding model -->

            <MetroUI:MetroDialog x:Name="dialogSettings" Visibility="Collapsed" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <Canvas>
                    <TextBlock FontSize="20" FontWeight="Bold" Canvas.Left="54">Oh Oh! An error has occurred</TextBlock>
                    <TextBlock Canvas.Left="16" Canvas.Top="55" Width="370" Text="{Binding ErrorString}" FontSize="16" TextWrapping="Wrap"></TextBlock>
                    <Button Style="{StaticResource WindowsMetroButtonStyle}" x:Name="btnCloseSettingsDialog" Content="Cancel" Click="btnCloseConnectDialog_Click" Visibility="{Binding Connected, Converter={StaticResource BoolToVisibilityConverter}}" Height="22" Width="73" Canvas.Left="250" Canvas.Top="136"></Button>
                </Canvas>
            </MetroUI:MetroDialog>

            <MetroUI:MetroDialog x:Name="dialogConnect" Visibility="Hidden" Grid.ColumnSpan="2" Grid.RowSpan="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DataContext="{Binding Device}">
                <Canvas>
                    <TextBlock Margin="23,30,428,0" Height="18" VerticalAlignment="Top" Canvas.Left="25" Canvas.Top="15">Connect to:</TextBlock>
                    <ComboBox Canvas.Left="115" Canvas.Top="40" ItemsSource="{Binding Devices}" SelectedItem="{Binding CurrentConnectableDevice, Mode=TwoWay}" HorizontalAlignment="Right" Width="293">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Name}"></TextBlock>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>

                    <Button Style="{StaticResource WindowsMetroButtonStyle}" x:Name="btnConnect" Content="Connect" Click="btnConnect_Click" Height="22" Width="73" Canvas.Left="333" Canvas.Top="136"></Button>
                    <Button Style="{StaticResource WindowsMetroButtonStyle}" x:Name="btnCloseConnectDialog" Content="Cancel" Click="btnCloseConnectDialog_Click"  Visibility="{Binding Connected, Converter={StaticResource BoolToVisibilityConverter}}" Height="22" Width="73" Canvas.Left="250" Canvas.Top="136"></Button>

                    <StackPanel Orientation="Horizontal" Height="60" Width="700" Canvas.Left="45" Canvas.Top="70" Visibility="{Binding IsError, Converter={StaticResource BoolToVisibilityConverter}}">
                        <Image Source="/images/error.png" Height="48" Width="24" VerticalAlignment="Top"></Image>
                        <ScrollViewer Margin="8,0,0,0" Width="330">
                            <TextBlock Text="{Binding StatusMessage}" TextWrapping="Wrap" Foreground="Red"></TextBlock>
                        </ScrollViewer>
                    </StackPanel>

                    <StackPanel Orientation="Horizontal" Height="40" Width="330" Canvas.Left="18" Canvas.Top="80" Visibility="{Binding Connected, Converter={StaticResource BoolToVisibilityConverter}}">
                        <Image Source="/images/connected.png" Height="48" Width="27" VerticalAlignment="Top"></Image>
                        <TextBlock Text="{Binding StatusMessage}" HorizontalAlignment="Stretch" Margin="8,0,0,0" VerticalAlignment="Stretch" TextWrapping="Wrap" Foreground="Green" Canvas.Left="18" Canvas.Top="36"></TextBlock>
                    </StackPanel>

                    <StackPanel x:Name="stackElevatedButton" Orientation="Horizontal" Canvas.Top="-10" Canvas.Left="275">
                        <Image Source="{x:Static stock:StockIcons.Shield}" Height="16" Width="16" Margin="0,0,6,0" Visibility="{Binding IsElevated, ElementName=TheWindow, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=True}"/>
                        <TextBlock Style="{StaticResource MetroSmallHeader}" VerticalAlignment="Center" Visibility="{Binding IsElevated, ElementName=TheWindow, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=True}">
                            <Hyperlink Style="{StaticResource MetroLink}" Name="btnLaunchElevated" Click="btnLaunchElevated_Click">LAUNCH ELEVATED</Hyperlink>
                        </TextBlock>
                        <TextBlock Margin="0,0,0,0" Style="{StaticResource MetroSmallHeader}" FontSize="12" VerticalAlignment="Center" Visibility="{Binding IsElevated, ElementName=TheWindow, Converter={StaticResource BoolToVisibilityConverter}}" Foreground="Red">
                        RUNNING ELEVATED
                        </TextBlock>
                    </StackPanel>

                </Canvas>
            </MetroUI:MetroDialog>

        </Grid>
    </Border>
</MetroControls:MetroWindow>
